<template>
  <n-modal v-model:show="isVisible" title="添加库存" preset="dialog" @close="onClose"@update:show="onClose" style="height: 400px;width: 700px">
    <n-form @submit.prevent="handleSubmit" label-width="120px" class="form-container">
      <div class="form-row" style="margin-top: 20px">
        <n-form-item label="仓库：" path="storeId">
          <n-select
          v-model:value="form.storeId"
          :options="stores"
          style="width: 180px"
          ></n-select>
        </n-form-item>
        <n-form-item label="产品：" path="productId">
            <n-select
                v-model:value="form.productId"
                :options="products"
                style="width: 180px"
            ></n-select>
        </n-form-item>
      </div>
      <div class="form-row">
        <n-form-item label="可用量：" path="quantity">
          <n-input v-model:value="form.quantity" placeholder="请输入实际可用量" />
        </n-form-item>
        <n-form-item label="期望可用量：" path="minimumQuantity">
          <n-input v-model:value="form.minimumQuantity" placeholder="请输入期望可用量" />
        </n-form-item>
      </div>
      <!-- 提交按钮 -->
      <div class="submit-row">
        <n-form-item>
          <n-button type="primary" native-type="submit" @click="handleSubmit">提交</n-button>
        </n-form-item>
      </div>
    </n-form>
  </n-modal>
</template>

<script setup>
import {onMounted, ref, watch} from 'vue';
import {useRouter} from 'vue-router';
import {useUserStore} from "@/store/index.js";
import api from '@/api/admin'
import {List} from "echarts";
import {formatDate, formatDateTime} from "@/utils";
import {NCascader} from 'naive-ui'

const router = useRouter()
const loading = ref(true)
const userStore = useUserStore()
const id = userStore.getId();
const stores=ref([])
const products=ref([])
const emit = defineEmits(['updatePage'])
const props = defineProps({
  show: {
    type: Boolean,
    required: true,
  },
  onClose: {
    type: Function,
    required: true,
  },
});

const isVisible = ref(props.show);
watch(() => props.show, (newVal) => {
  isVisible.value = newVal;
});

const form = ref({
  storeId:'',
  productId:'',
  quantity:'',
  minimumQuantity:'',
});
const handleSubmit = async () => {
  try {
    const res = await api.addInventory(
      {
        storeId:form.value.storeId,
        productId:form.value.productId,
        quantity:form.value.quantity,
        minimumQuantity:form.value.minimumQuantity,
        status:'enough',
      }
    )
    if(res.code === 200) {
      emit("updatePage")
    }
    else {
      $message.error(res.message)
    }
  } catch (err) {
    $message.error("添加失败！")
  }finally{
    clear()
    props.onClose();
  }
};
const queryStoreList = async () => {
  loading.value = true
  try {
    const res = await api.findAllStore()
    if(res.code === 200) {
      res.data.map((item)=>{
        stores.value.push({
          label: item.name,
          value: item.id
        })
      })
    }
    else {
      $message.error(res.message)
    }
  } catch (err) {
    $message.error("获取仓库信息失败！")
  }finally{
    loading.value = false
  }
}
const queryProductList = async () => {
  loading.value = true
  try {
    const res = await api.findAllProduct()
    if(res.code === 200) {
      res.data.map((item)=>{
        products.value.push({
          label: item.name,
          value: item.id
        })
      })
    }
    else {
      $message.error(res.message)
    }
  } catch (err) {
    $message.error("获取产品信息失败！")
  }finally{
    loading.value = false
  }
}

const clear = () => {
  form.value.storeId='',
    form.value.productId='',
    form.value.quantity='',
    form.value.minimumQuantity=''
}
onMounted(()=>{
  queryStoreList()
  queryProductList()
})
</script>

<style scoped>
    .form-container {
        display: flex;
        flex-direction: column;
        gap: 20px;
        width: 100%;
        position: relative;
    }

    .form-row {
        display: flex;
        gap: 20px;
        align-items: center;
        width: 100%;
    }

    .form-row > n-form-item {
        flex: 1;
    }

    .submit-row {
        justify-content: center;
        display: flex;
        width: 100%;
    }
</style>
